﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Fitment.App_Code;

namespace Fitment.User
{
    public partial class proDatePlan : System.Web.UI.Page
    {
        DS data = new DS();
        protected void Page_Load(object sender, EventArgs e)
        {
            //如果Session为空,则转到登陆页.
            if (Session["User_ID"] == null)
            {
                Response.Redirect("../logon.aspx");
            }
            if (Session["House_ID"] == null)
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "", " <SCRIPT>if(confirm('您还没有输入要装修的房子信息,请选择房子进行编辑或者重新输入房子信息！')) window.location.href = 'myHouse.aspx';</SCRIPT>");
            }
        }

        protected void OK_Click(object sender, EventArgs e)
        {
            int houseId = Convert.ToInt32(Session["House_ID"].ToString());
            DateTime startDate = DateTime.Parse(Session["Date"].ToString());

            string sql = "SELECT  WORKERINFO.WORKTYPE, WORKERINFO.PRIORITY, SUM(MATERIAL.NEEDTIME) AS TOTALTIME "
                + "FROM MATERIAL, ROOMINFO, SELECTITEM, WORKERINFO "
                + "WHERE ROOMINFO.HOUSE_ID = '" + houseId + "'  AND ROOMINFO.ROOM_ID = SELECTITEM.ROOM_ID "
                + "AND SELECTITEM.MAT_ID = MATERIAL.MAT_ID AND MATERIAL.WORKTYPE = WORKERINFO.WORKTYPE "
                + "GROUP BY WORKERINFO.WORKTYPE, WORKERINFO.PRIORITY ORDER BY WORKERINFO.PRIORITY ASC";
            DataTable table = data.GetDataSource(sql, "info");
            int count = table.Rows.Count;
            ProDateMng[] proDate = new ProDateMng[count];
            int prio = -1;  //上一个工种的优先级
            int curPr = 0; //当前工种的优先级,值越小，优先级越高
            int day = 0;
            for (int i = 0; i < count; i++)
            {
                proDate[i] = new ProDateMng();
                proDate[i].HouseId = houseId;
                proDate[i].WorkType = table.Rows[i][0].ToString();  //工种类型
                curPr = Convert.ToInt32(table.Rows[i][1].ToString());  //优先级
                if (prio < curPr)
                {
                    prio = curPr;  //当前工种优先级低于上一个工种优先级（值越小，优先级越高）
                    startDate = startDate.AddDays(day);  //增加上次工种施工的天数作为这次的开始时间
                    proDate[i].StartDate = startDate;
                    day = Convert.ToInt32(table.Rows[i][2].ToString());  //该工种需要施工时间
                }
                else //当前工种优先级等于上一个工种优先级
                {
                    proDate[i].StartDate = startDate;  //用上个工种施工的开始日期作为这次的开始日期
                    if (day < Convert.ToInt32(table.Rows[i][2].ToString()))
                        day = Convert.ToInt32(table.Rows[i][2].ToString());//取优先级相同的工种施工日期最长的作为该优先级的持续时间
                }
                proDate[i].ArrangeDate();
            }
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('工程项目日期安排成功')</script>");
        }
    }
}
